package com.xukai.springboot.controller.statistics;

import com.xukai.springboot.entity.request.statis.SaveDataSourceRequest;
import com.xukai.springboot.entity.response.ResponseBase;
import com.xukai.springboot.entity.statistics.t_statis_chart_datasource;
import com.xukai.springboot.service.statistics.ChartDataSourceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Options;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.xml.ws.Response;
import java.util.List;

@Api(basePath = "api/chart/dataSourceSetting", description = "图表设置数据源管理")
@RestController
@RequestMapping("api/chart/dataSourceSetting")
public class ChartDataSourceController {

    /**
     * 日志
     */
    private final Logger logger = LoggerFactory.getLogger(ChartDataSourceController.class);


    @Autowired
    ChartDataSourceService chartDataSourceService;

    /**
     * 获取数据源列表
     */
    @ApiOperation(value = "获取数据源列表", notes = "获取数据源列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "chartID", value = "图表ID", required = true, dataType = "int", example = "0", defaultValue = "0", paramType = "query")
    })
    @GetMapping("GetDataSourceList")
    public ResponseBase<List<t_statis_chart_datasource>> GetDataSourceList(@RequestParam int chartID) {
        ResponseBase<List<t_statis_chart_datasource>> result = new ResponseBase<>();
        try {
            List<t_statis_chart_datasource> data = chartDataSourceService.GetDataSourceList(chartID);
            result.setData(data);
        } catch (Exception ex) {
            logger.error("获取数据源列表", ex);
            result.setStatue(0);
        }
        return result;
    }


    /**
     * 保存数据源
     */
    @ApiOperation(value = "保存数据源", notes = "保存数据源")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "param", value = "参数", required = true, dataType = "SaveDataSourceRequest", paramType = "body")
    })
    @PostMapping("SaveDataSource")
    public ResponseBase SaveDataSource(@RequestBody SaveDataSourceRequest param) {
        ResponseBase result = new ResponseBase();

        try {
            chartDataSourceService.SaveDataSource(param.getChartID(), param.getDataList());
        } catch (Exception ex) {
            logger.error("保存数据源", ex);
            result.setStatue(0);
        }
        return result;
    }
}
